package com.example.material.view;

import android.animation.AnimatorSet;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Property;
import android.view.MotionEvent;
import android.view.animation.DecelerateInterpolator;
import android.widget.RelativeLayout;

import com.example.material.R;

/**
 * 触碰时带波纹效果的相对布局
 * @author zhuyb
 * @date 2015-6-16
 * @version V1.0.0
 */
public class RippleLayout extends RelativeLayout {
	private Paint paint; // 波纹画笔
	private float cx, cy, radius; // 波纹圆点、半径
	private int rippleAlpha = 90;
	private int bgColor; // 布局背景色
	private int rippleColor = Color.BLACK; // 波纹颜色
	
	public RippleLayout(Context context) {
		super(context);
		init(null, 0);
	}
	
	public RippleLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(attrs, 0);
	}

	public RippleLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(attrs, defStyle);
	}
	
	private void init(AttributeSet attrs, int defStyle) {
		// Load attributes
		final TypedArray typeArray = getContext().obtainStyledAttributes(attrs, R.styleable.RippleLayout,
				defStyle, 0);
		rippleAlpha = typeArray.getColor(R.styleable.RippleLayout_rl_alpha, rippleAlpha);
		bgColor = typeArray.getColor(R.styleable.RippleLayout_rl_bg_color, bgColor);
		rippleColor = typeArray.getColor(R.styleable.RippleLayout_rl_ripple_color, rippleColor);
		typeArray.recycle();
		this.setBackgroundColor(bgColor);
		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setStyle(Paint.Style.FILL);
		paint.setAlpha(rippleAlpha);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		canvas.save();
		canvas.drawCircle(cx, cy, radius, paint);
		canvas.restore();
		super.onDraw(canvas);
	}
	
	@SuppressLint("ClickableViewAccessibility")
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			// 记录触碰坐标
			cx = event.getX();
			cy = event.getY();
			// 启动动画
			startAnimation();
		}
		return super.onTouchEvent(event);
	}

	@SuppressWarnings("unchecked")
	private void startAnimation() {
		// 计算半径变化区域
		int start, end;
		if (getHeight() < getWidth()) {
			start = getHeight();
			end = getWidth();
		} else {
			start = getWidth();
			end = getHeight();
		}
		float startRadius = (start / 2  > cy ? start - cy : cy) * 1.15f;
		float endRadius = (end / 2 > cx ? end - cx : cx) * 1f;
		// 新建动画
		AnimatorSet set = new AnimatorSet();
		// 添加变化属性
		set.playTogether(
			// 波纹半径变化
			ObjectAnimator.ofFloat(this, radiusProperty, startRadius, endRadius),
			// 波纹颜色变化 波纹颜色到透明
			ObjectAnimator.ofObject(this, rippleColorProperty, new ArgbEvaluator(), rippleColor, Color.TRANSPARENT)
		);
		// 设置时间
		set.setDuration((long) (1800 / end * endRadius));
		// 先快后慢
		set.setInterpolator(new DecelerateInterpolator());
		set.start();
	}
	
	// 半径属性
	private Property<RippleLayout, Float> radiusProperty = new Property<RippleLayout, Float>(Float.class, "radius") {
		@Override
		public Float get(RippleLayout object) {
			return object.radius;
		}
		@Override
		public void set(RippleLayout object, Float value) {
			object.radius = value;
			// 刷新画布
			invalidate();
		}
	};
	
	// 波纹颜色属性
	private Property<RippleLayout, Integer> rippleColorProperty = new Property<RippleLayout, Integer> (Integer.class, "bg_color") {
		@Override
		public Integer get(RippleLayout object) {
			return object.paint.getColor();
		}
		@Override
		public void set(RippleLayout object, Integer value) {
			object.paint.setColor(value);
		}
	};
}
